﻿using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Fatura
{
    public class ParseExcel
    {
        public enum Direction
        {
            Right = 1,
            Left = 2,
            Up = 3,
            Down = 4
        }

        public enum Stop
        {
            Değer_okuyunca = 1,
            Boş_görünce = 2,
        }

        public enum Action
        {
            Bul = 1,
            Yön_Belirle = 2,
            Fatura_Satırı = 3,
            Alt_Toplamları_Çek = 4,
        }

        public RepositoryItemGridLookUpEdit riAction
        {
            get
            {
                DataTable dt = EnumToDataTable(typeof(Action));

                RepositoryItemGridLookUpEdit ri = RepositoryManager.GetGLESablon(dt, "Name", "Id");
                return ri;
            }
        }

        public RepositoryItemGridLookUpEdit riEmpty
        {
            get
            {
                DataTable dt = EnumToDataTable(typeof(Stop));

                RepositoryItemGridLookUpEdit ri = RepositoryManager.GetGLESablon(dt, "Name", "Id");
                return ri;
            }
        }
           
        public RepositoryItemGridLookUpEdit riDirection
        {
            get
            {
                DataTable dt = EnumToDataTable(typeof(Direction));

                RepositoryItemGridLookUpEdit ri = RepositoryManager.GetGLESablon(dt, "Name", "Id");
                return ri;
            }
        }
        
        public DataTable EnumToDataTable(Type enumType)
        {
            DataTable table = new DataTable();

            //Column that contains the Captions/Keys of Enum        
            table.Columns.Add("Name", typeof(string));
            //Get the type of ENUM for DataColumn
            table.Columns.Add("Id", Enum.GetUnderlyingType(enumType));
            //Add the items from the enum:
            foreach (string name in Enum.GetNames(enumType))
            {
                //Replace underscores with space from caption/key and add item to collection:
                table.Rows.Add(name.Replace('_', ' '), Enum.Parse(enumType, name));
            }

            return table;
        }

        public Point FindPositionByValue(DataTable dt, string value)
        {
            Point loc = new Point();

            bool OuterBreak = false;
            for (int dc = 0; dc < dt.Columns.Count; dc++)
            {
                string ColumnName = dt.Columns[dc].ColumnName;
                for (int dr = 0; dr < dt.Rows.Count; dr++)
                {
                    string CellValue = dt.Rows[dr][ColumnName].ToStr();
                    if (CellValue.ToLower() == value.ToLower())
                    {
                        loc.X = dr;
                        loc.Y = dc;
                        OuterBreak = true;
                        break;
                    }
                }
                if (OuterBreak) break;
            }
            return loc;
        }

        public string GetValueByLocation(DataTable dt, Point pt,Direction direction,bool toEmpty)
        {
            string Value="";

            int seed = 1;
            if (direction == Direction.Up || direction == Direction.Left)
                seed = -1;


            if (direction == Direction.Up || direction == Direction.Down)
            {
                for (int dr = pt.X + seed; dr < dt.Rows.Count; dr += seed)
                {
                    string CellValue = dt.Rows[dr][pt.Y].ToStr();
                    if (CellValue != "")
                    {
                        Value += CellValue + " ";
                        if (!toEmpty) break;
                    }
                    else
                    {
                        if (toEmpty) break;
                    }
                }
            }
            else
            {
                for (int dc = pt.Y+seed; dc < dt.Columns.Count; dc+=seed)
                {
                    string CellValue = dt.Rows[pt.X][dc].ToStr();
                    if (CellValue != "")
                    {
                        Value += CellValue + " ";
                        if (!toEmpty) break;
                    }
                    else
                    {
                        if (toEmpty) break;
                    }
                }
            }
            return Value;
        }

        public string GetValue(DataTable dt, string Caption, Direction direction)
        {
            string Value = "";

            Point pt = FindPositionByValue(dt, Caption);

            if (pt.X != 0 || pt.Y != 0)
            {
                int seed = 1;
                int ValueIndex = pt.X;

                if (direction == Direction.Up || direction == Direction.Left)
                {
                    seed = -1;
                }

                if (direction == Direction.Up || direction == Direction.Down)
                {
                    for (int dr = pt.X; dr < dt.Rows.Count; dr += seed)
                    {
                        string CellValue = dt.Rows[dr][pt.Y + 1].ToStr();
                        string CaptionValue = dt.Rows[dr][pt.Y].ToStr();

                        if ((CaptionValue != "" && Caption != CaptionValue) || CellValue == "")
                            break;
                        if (CellValue != "")
                            Value += CellValue + " ";
                    }
                }
            }
            return Value;
        }

        public string GetValue(DataTable dt,string Caption)
        {
            Point loc = FindPositionByValue(dt, Caption);
            string Value = "";
            if (loc.X != 0 || loc.Y != 0)
                Value = GetValueByLocation(dt, loc, Direction.Right, false);
            return Value;
        }

        public string GetValue(DataTable dt, string Caption,Direction direction,bool toEmpty)
        {
            Point loc = FindPositionByValue(dt, Caption);
            string Value = "";
            if (loc.X != 0 || loc.Y != 0)
                Value = GetValueByLocation(dt, loc, direction, toEmpty);
            return Value;
        }

        public DataTable GetInvoiceLines(DataTable dt,string Value)
        {
            DataTable DtRetreived = new DataTable();

            Point pt = FindPositionByValue(dt, Value);
            List<string> Captions = new List<string>();
            int FirstColumnIndex = 0;
            int emptyCell = 0;

            for (int dc = pt.Y ; dc >= 0; dc--)
            {
                string CellValue = dt.Rows[pt.X][dc].ToStr();
                if (CellValue != "")
                    Captions.Add(CellValue);
                else
                {
                    emptyCell++;
                    if (emptyCell == 1)
                        Captions.Add("Unit");
                    else
                        break;
                }
                FirstColumnIndex = dc;
            }

            foreach (string Caption in Captions.Reverse<string>())
            {
                DtRetreived.Columns.Add(Caption);
            }

            bool OuterBreak = false;
            for (int dr = pt.X + 1; dr < dt.Rows.Count; dr++)
            {
                DataRow newRow = DtRetreived.NewRow();
                for (int dc = FirstColumnIndex; dc <= pt.Y; dc++)
                {
                    string ColumnName = DtRetreived.Columns[dc-FirstColumnIndex].ColumnName;
                    string CellValue = dt.Rows[dr][dc].ToStr();

                    if (ColumnName.ToLower() == "total  value" && CellValue == "")
                    {
                        OuterBreak = true;
                        break;
                    }
                    newRow[ColumnName] = dt.Rows[dr][dc].ToStr();
                }
                if (OuterBreak)
                    break;

                DtRetreived.Rows.Add(newRow);
            }

            return DtRetreived;
        }
        
        public DataTable GetSubTotal(DataTable dt, string Value,bool WithRightCell)
        {
            DataTable DtRetreived = new DataTable();
            DtRetreived.Columns.Add("Total", typeof(decimal));
            DtRetreived.Columns.Add("TotalDesc", typeof(string));

            Point pt = FindPositionByValue(dt, Value);
            
            bool IsEndOfInvoiceLines = false;
            for (int dr = pt.X + 1; dr < dt.Rows.Count; dr++)
            {
                decimal value = Math.Abs(dt.Rows[dr][pt.Y].ToDecimal());
                if (value > 0 && !IsEndOfInvoiceLines)
                    continue;
                IsEndOfInvoiceLines = true;

                if (value > 0)
                {
                    string caption = dt.Rows[dr][pt.Y - 1].ToStr();
                    string desc = WithRightCell ? " ("+dt.Rows[dr][pt.Y + 1].ToStr() +")": "";
                    string totalDesc = caption+desc;
                    DtRetreived.Rows.Add(value,totalDesc);
                }
            }

            return DtRetreived;
        }
    }
}
